package exp.day03;

//实现分数类，完成1/1+1/2+....+1/20的精确值
public class P01 {
    long up, down;

    static long gcd(long a, long b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    public P01() {
        this(0,1);
    }

    public P01(long up, long down) {
        long g = gcd(up, down);
        up /= g;
        down /= g;
        this.up = up;
        this.down = down;
    }

    @Override
    public String toString() {
        return String.format("%d/%d", this.up, this.down);
    }

    P01 add(P01 p1) {
        P01 p2 = new P01();
        p2.up = this.up * p1.down + this.down * p1.up;
        p2.down = this.down * p1.down;
        long g = gcd(p2.up, p2.down);
        p2.up /= g;
        p2.down /= g;
        return  p2;
    }
}
